package com.zcl.Thread.t17;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description:
 * @author: zcl
 * @date: 2022/3/10  19:17
 * <p>
 * 创建线程的方式4:创建线程池
 */
class NumberThread implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i%2==0){
                System.out.println(i);
            }
        }
    }
}


public class ThreadPool {

    public static void main(String[] args) {
        //Executors.newCachedThreadPool(): 创建一个可根据需要创建新线程的连接池
        //Executors.newFixedThreadPool(n): 创建一个可重用固定线程数的线程池
        //Executors.newSingleThreadExecutor(): 创建一个只有一个线程的线程池
        //Executors.newScheduledThreadPool(n): 创建一个线程池,它可安排在给定延迟后运行命令或定期地执行

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        NumberThread numberThread = new NumberThread();

        //适合用于Runnable
        executorService.execute(numberThread);

        //适合用于Callable
        //executorService.submit();
        //ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor();

        //关闭连接池
        executorService.shutdown();
    }
}
